package org.springblade.common.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springblade.common.utils.delayed.DelayedSend;
import org.springblade.common.utils.delayed.StopElevatorSend;
import org.springblade.common.utils.delayed.WxWarnSend;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description
 * @Author ysq
 * @Date 2023/6/21
 **/
@Component
public class MqMessageService {

	@Resource
	private ProdMessageSend prodMessageSend;
	@Resource
	private TestMessageSend testMessageSend;
	@Resource
	private Environment environment;
	@Resource
	private RescueMessageSend rescueMessageSend;
	@Resource
	private DelayedSend delayedSend;
	@Resource
	private StopElevatorSend stopElevatorSend;
	@Resource
	private WxWarnSend wxWarnSend;

	public void send(String message) {
		//当前环境
		String curEnvironment = environment.getActiveProfiles()[0];
		if (Func.equalsSafe(curEnvironment, "prod")) {
			// 正式环境
			prodMessageSend.rescueMessage(message);
		} else {
			// 测试环境
			testMessageSend.rescueMessage(message);
		}
	}

	public void sendRescueArrive(long time, String message) {
		//当前环境
		String curEnvironment = environment.getActiveProfiles()[0];
		if (Func.equalsSafe(curEnvironment, "prod")) {
			// 正式环境
			delayedSend.sendRescueArriveMsg(time, message);
		} else if (Func.equalsSafe(curEnvironment, "test")) {
			// 测试环境
			delayedSend.sendRescueMsg(time,message);
		}
	}

	public void sendElevatorStopMsg(long time, String message) {
		//当前环境
		String curEnvironment = environment.getActiveProfiles()[0];
		if (Func.equalsSafe(curEnvironment, "prod")) {
			// 正式环境
			stopElevatorSend.sendElevatorStopMsg(time, message);
		} else if (Func.equalsSafe(curEnvironment, "test")) {
			// 测试环境
			stopElevatorSend.sendElevatorStopTestMsg(time, message);
		}
	}


	/**
	 * 发送维修预警消息通知
	 *
	 * @param time    时间
	 * @param message 消息体
	 */
	public void sendWxWarnMsg(long time, String message) {
		//当前环境
		String curEnvironment = environment.getActiveProfiles()[0];
		JSONObject jsonObject = JSON.parseObject(message);
		jsonObject.put("environment", curEnvironment);
		jsonObject.put("tenantId", AuthUtil.getTenantId());
		wxWarnSend.sendWxWarnMsg(time, jsonObject.toJSONString());
	}


	/**
	 * 紧急呼叫
	 *
	 * @param message
	 */
	public void sendRescueTaskMessage(String message) {
		rescueMessageSend.rescueTaskMessage(message);
	}

}
